$(function () {

    getUserInfo()

})

// 获取用户信息
function getUserInfo() {

    axios({
        method: 'GET',
        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem('token')
        // }
    }).then(function (res) {
        console.log(res.data.data);
        if (res.data.status !== 0) {
            return layui.layer.msg(res.data.message)
        }

        renderAvatar(res.data.data)
    })


}

// 渲染页面
function renderAvatar(user) {

    let name = user.nickname || user.username
    $('.welcome').html('欢迎&nbsp' + name)

    if (user.user_pic !== null) {

        $('.avatar-text').hide()
        $('.layui-nav-img').show().attr('src', user.user_pic)
    } else {

        $('.layui-nav-img').hide()
        let first = name[0].toUpperCase()
        $('.avatar-text').show().html(first)

    }

}